<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>controller</title>
    <link rel="stylesheet" href="../lib/bootstrap3/css/bootstrap.min.css">
    <script src="../lib/jquery/jquery-1.10.2.js"></script>
    <script src="../lib/angularjs/angular.min.js"></script>
    <style>
        body{padding:50px 200px;}
    </style>
    <script>
        var app=angular.module("app01",[]);
    </script>
</head>
<body ng-app="app01">
<h3>ngModel提供更底层的API来修改控制器内的数据</h3>
<DIV CLASS="panel panel-success">
    <div class="panel-heading">例子一</div>
    <div class="panel-body">
        <div class="container" ng-controller="MyController">
            <cust ng-model="name" atts="{{name}}"></cust>
            <cust ng-model="age" atts="{{age}}"></cust>
        </div>
    </div>
    <script>
        app.controller("MyController", ['$scope', function($scope) {
            $scope.name = "mario";
            $scope.age = "13";
        }]);
        app.directive("cust", function() {
            return {
                restrict : 'AE', require : 'ngModel', replace : true,
                scope : {model : '@atts'},
                template : '<div ng-click="change()">{{model}}</div>',
                link : function(scope, elem, attrs, ctrl) {
                    scope.change = function() {
                        alert(ctrl.$viewValue);
                        ctrl.$setViewValue(scope.model + '123');
                    };
                }
            }
        });
    </script>
</DIV>
<DIV CLASS="panel panel-success">
    <div class="panel-heading">自定义渲染</div>
    <div class="panel-body">
        <myDirective2></myDirective2>
    </div>
    <script>
        app.directive('myDirective2', function() {
            return {
                require: '?ngModel',
                link: function(scope, ele, attrs, ngModel) {
                    if (!ngModel) return;
                    ngModel.$render = function() {
                        element.html(ngModel.$viewValue() || 'None');
                    };
                }
            };
        });
    </script>
</DIV>
</body>
</html>
